﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTaskItemDetail.xaml
    /// </summary>
    public partial class VMTaskItemDetail : UserControl
    {
        public VMTaskItemDetail()
        {
            InitializeComponent();
            SetupEvent();
        }
        Step_TaskItem _Step_TaskItem_Org;
        public void SetupDataContext(Step_TaskItem data)
        {
            if (data == null)
            {
                MessageBox.Show("data == null. Xin thông báo tới chúng tôi để sửa lỗi");
                return;
            }
            _Step_TaskItem_Org = data;
            Step_TaskItem newData = CloneHelper.CloneHelper.CloneWithSerializer<Step_TaskItem>(data);
            this.DataContext = newData;
        }

        private void SetupEvent()
        {
            Button_OK.Click += new RoutedEventHandler(Button_OK_Click);
            Button_Cancel.Click += new RoutedEventHandler(Button_Cancel_Click);
        }

        void Button_OK_Click(object sender, RoutedEventArgs e)
        {
            if (_Step_TaskItem_Org == null)
            {
                MessageBox.Show("_Step_TaskItem_Org == null. Xin thông báo tới chúng tôi để sửa lỗi");
            }
            Step_TaskItem current = this.DataContext as Step_TaskItem;
            if (current == null)
            {
                MessageBox.Show("current == null. Xin thông báo tới chúng tôi để sửa lỗi");
            }
            //Copy
            var props = typeof(Step_TaskItem).GetProperties();
            foreach (var p in props)
            {
                if (p.CanRead && p.CanWrite)
                {
                    var v = p.GetValue(current, null);
                    p.SetValue(_Step_TaskItem_Org, v, null);
                }
            }
            Close();
        }

        void Button_Cancel_Click(object sender, RoutedEventArgs e)
        {
            
            Close();
        }

        private void Close()
        {
            var w = DependencyHelper.GetAncestorByTypeOrBaseType(this, typeof(Window)) as Window;
            if (w != null)
                w.Close();
        }
        
    }
}
